import redis
import json

# 连接Redis数据库
db0 = redis.Redis(host='127.0.0.1', port=6379, db=1, decode_responses=True)
db1 = redis.Redis(host='127.0.0.1', port=6379, db=2, decode_responses=True)

# 清空现有数据
db0.delete('student')
db1.flushdb()

# 学生数据
students = [
    {
        'schoolnumber': '20230101111',
        'name': '张三',
        'password': '123456',
        'scores': [
            # 1月成绩
            {'month': 1, 'chinese': 85, 'math': 88, 'english': 90, 
             'physics': 87, 'chemistry': 89, 'biology': 86},
            # 3月成绩
            {'month': 3, 'chinese': 88, 'math': 92, 'english': 89, 
             'physics': 90, 'chemistry': 91, 'biology': 88},
            # 6月成绩
            {'month': 6, 'chinese': 92, 'math': 95, 'english': 93, 
             'physics': 94, 'chemistry': 92, 'biology': 91}
        ]
    },
    {
        'schoolnumber': '20230102222',
        'name': '李四',
        'password': '123456',
        'scores': [
            # 2月成绩
            {'month': 2, 'chinese': 75, 'math': 78, 'english': 72, 
             'physics': 76, 'chemistry': 77, 'biology': 74},
            # 4月成绩
            {'month': 4, 'chinese': 79, 'math': 82, 'english': 77, 
             'physics': 80, 'chemistry': 81, 'biology': 78}
        ]
    },
    {
        'schoolnumber': '20230103333',
        'name': '王五',
        'password': '123456',
        'scores': [
            # 1月成绩
            {'month': 1, 'chinese': 95, 'math': 98, 'english': 96, 
             'physics': 97, 'chemistry': 94, 'biology': 95},
            # 5月成绩
            {'month': 5, 'chinese': 98, 'math': 99, 'english': 97, 
             'physics': 98, 'chemistry': 96, 'biology': 97}
        ]
    },
    {
        'schoolnumber': '20230104444',
        'name': '赵六',
        'password': '123456',
        'scores': [
            # 2月成绩
            {'month': 2, 'chinese': 65, 'math': 68, 'english': 70, 
             'physics': 67, 'chemistry': 69, 'biology': 66},
            # 4月成绩
            {'month': 4, 'chinese': 70, 'math': 72, 'english': 75, 
             'physics': 71, 'chemistry': 73, 'biology': 70},
            # 6月成绩
            {'month': 6, 'chinese': 75, 'math': 77, 'english': 79, 
             'physics': 76, 'chemistry': 78, 'biology': 75}
        ]
    }
]

# 写入数据
for student in students:
    # 写入登录信息
    db1.set(student['schoolnumber'], student['password'])
    
    # 写入成绩信息
    for score in student['scores']:
        data = {
            'schoolnumber': student['schoolnumber'],
            'name': student['name'],
            'chinese': score['chinese'],
            'math': score['math'],
            'english': score['english'],
            'physics': score['physics'],
            'chemistry': score['chemistry'],
            'biology': score['biology'],
            'total': sum([score['chinese'], score['math'], score['english'],
                         score['physics'], score['chemistry'], score['biology']]),
            'month': score['month']
        }
        # 使用复合键：学号_月份
        key = f"{student['schoolnumber']}_{score['month']}"
        db0.hset('student', key, json.dumps(data))

print('测试数据初始化完成')